package org.wddsicmd.widgets;
 
import org.wddsicmd.app.CodeEditorMain;
import org.wddsicmd.app.ButtonTabComponent;
import org.wddsicmd.dev.AndroidDeviceManager;
import org.wddsicmd.util.ResourceBundleUtil;
import org.wddsicmd.util.Log;
import org.wddsicmd.util.Resource;
import org.wddsicmd.widgets.OperationButton;

import java.lang.Object;

import java.io.*;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.util.*;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;

import java.awt.*;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.Insets;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.Image;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.Timer;
import javax.swing.JTable;
import javax.swing.table.*;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.Icon;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.event.TreeModelListener;
import javax.swing.filechooser.FileSystemView;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.Document;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;

import javax.swing.Icon;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
import javax.swing.plaf.basic.BasicGraphicsUtils;

public class OthersPanel extends JPanel  implements ActionListener {

	private final static String TAG = "OthersPanel";

	private final static String BUTTON_CREATE_ALL_DIRS = "create_all_dirs";
	private final static String BUTTON_DELETE_ALL_DIRS = "delete_all_dirs";
	
	private OperationButton mCreateDirButton;
	private OperationButton mDeleteDirButton;
	
	private CodeEditorMain mCodeEditorMain;
	
	public OthersPanel(CodeEditorMain cem, String titleStr){
		super();
		mCodeEditorMain = cem;
		setLayout(null);
		setBackground(Resource.getBackgroundColor());
		addComponentListener(new ComponentAdapter() {
			@Override
			public void componentResized(ComponentEvent e) {
				onComponentResized();
			}
		});		
/* doom-2015-10-26 11:24:28 
		setBorder(BorderFactory.createEmptyBorder(50,50,50,50));
		setBorder(BorderFactory.createLineBorder(Resource.getForegroundColor()));
 doom-2015-10-26 11:24:28 */


/* doom-2015-11-2 15:2:0 
		JLabel testLabel = new JLabel(titleStr);
		testLabel.setBackground(Resource.getHighlightColor());
		testLabel.setBounds(new Rectangle(0, 0 , 200, 200));
		add(testLabel);
 doom-2015-11-2 15:2:0 */

		initViews();
		
	}
	public void onComponentResized(){
		int width = getWidth();
		int height = getHeight();

/* doom-2015-11-2 15:2:0 
		Log.d(TAG, "onComponentResized width: "+ width);
		Log.d(TAG, "onComponentResized height: "+ height);
 doom-2015-11-2 15:2:0 */
 		int paddingTop = 30;
		int gapVertical = 5;
		int contentwidth = width/6;
		int contentheight = (height-paddingTop)/8;

		mCreateDirButton.setBounds(new Rectangle(0, paddingTop, contentwidth, contentheight));
		
		mDeleteDirButton.setBounds(new Rectangle(0, paddingTop + contentheight, contentwidth, contentheight));
	}
	private void initViews(){
		mCreateDirButton = new OperationButton("\u4e00\u952e\u521b\u5efa\u7ec8\u7aef\u76ee\u5f55", 1);
		mCreateDirButton.setActionCommand(BUTTON_CREATE_ALL_DIRS);
		mCreateDirButton.addActionListener(this);	
		mCreateDirButton.setFont(new java.awt.Font("Dialog",  0,  15));   		
		add(mCreateDirButton);
		
		mDeleteDirButton = new OperationButton("\u4e00\u952e\u6e05\u9664\u7ec8\u7aef\u76ee\u5f55", 1);
		mDeleteDirButton.setActionCommand(BUTTON_DELETE_ALL_DIRS);
		mDeleteDirButton.addActionListener(this);	
		mDeleteDirButton.setFont(new java.awt.Font("Dialog",  0,  15));
/* doom-2015-11-3 11:59:49 
		mDeleteDirButton.setVisible(false);
 doom-2015-11-3 11:59:49 */
		add(mDeleteDirButton);
	}
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		if(command != null){
			if(command.equals(BUTTON_CREATE_ALL_DIRS)){
				createAllDirsHandler();
			}else if(command.equals(BUTTON_DELETE_ALL_DIRS)){
				deleteAllDirsHandler();
			}
		}
	}	

	private void createAllDirsHandler(){
		Log.d(TAG, "createAllDirsHandler ");

		adbCmdExec(" shell mkdir /sdcard/MSSLcdTest");
		adbCmdExec(" shell mkdir /sdcard/Movies");
		adbCmdExec(" shell mkdir /sdcard/OTP");
		
		adbCmdExec(" shell mkdir /sdcard/MSSLcdTest/driver");
		adbCmdExec(" shell echo \"\" > /sdcard/MSSLcdTest/driver/Version");
		adbCmdExec(" shell echo \"\" > /sdcard/MSSLcdTest/driver/LCDTiming");
		adbCmdExec(" shell echo \"\" > /sdcard/MSSLcdTest/driver/IDsetting");
		adbCmdExec(" shell echo \"\" > /sdcard/MSSLcdTest/driver/LCDinit1");
		adbCmdExec(" shell echo \"\" > /sdcard/MSSLcdTest/driver/LCDinit2");
		adbCmdExec(" shell echo \"\" > /sdcard/MSSLcdTest/driver/LCDinit3");
		adbCmdExec(" shell echo \"\" > /sdcard/MSSLcdTest/driver/LCDinit4");
		adbCmdExec(" shell echo \"\" > /sdcard/MSSLcdTest/driver/LCDinit5");
		adbCmdExec(" shell echo \"\" > /sdcard/MSSLcdTest/driver/LCDinit6");
		adbCmdExec(" shell echo \"\" > /sdcard/MSSLcdTest/driver/LCDinit7");
		adbCmdExec(" shell echo \"\" > /sdcard/MSSLcdTest/driver/LCDinit8");
		adbCmdExec(" shell echo \"\" > /sdcard/MSSLcdTest/driver/LCDsuspend");
		adbCmdExec(" shell echo \"\" > /sdcard/MSSLcdTest/driver/LCDResume");
		adbCmdExec(" shell echo \"\" > /sdcard/MSSLcdTest/driver/LCDPoweron");
		adbCmdExec(" shell echo \"\" > /sdcard/MSSLcdTest/driver/LCDPoweroff");
		adbCmdExec(" shell echo \"\" > /sdcard/MSSLcdTest/driver/HSwrite1");
		adbCmdExec(" shell echo \"\" > /sdcard/MSSLcdTest/driver/HSwrite2");
		adbCmdExec(" shell echo \"\" > /sdcard/MSSLcdTest/driver/HSwrite3");
		adbCmdExec(" shell echo \"\" > /sdcard/MSSLcdTest/driver/HSwrite4");
		adbCmdExec(" shell echo \"\" > /sdcard/MSSLcdTest/driver/HSwrite5");
		adbCmdExec(" shell echo \"\" > /sdcard/MSSLcdTest/driver/HSwrite6");
		adbCmdExec(" shell echo \"\" > /sdcard/MSSLcdTest/driver/HSwrite7");
		adbCmdExec(" shell echo \"\" > /sdcard/MSSLcdTest/driver/HSwrite8");
		adbCmdExec(" shell echo \"\" > /sdcard/MSSLcdTest/driver/HSread");		
		
		adbCmdExec(" shell ls /sdcard/");
	}
	private void deleteAllDirsHandler(){
		Log.d(TAG, "deleteAllDirsHandler ");

		adbCmdExec(" shell rm -rf /sdcard/MSSLcdTest");
		adbCmdExec(" shell rm -rf /sdcard/Movies");
		adbCmdExec(" shell rm -rf /sdcard/OTP");
		adbCmdExec(" shell ls /sdcard/");
	}	

	private void adbCmdExec(String cmdStr){

		if(mCodeEditorMain != null){
			String deviceName = mCodeEditorMain.getConnectedDevice();
			if(deviceName == null || deviceName.equals("")){
				Log.d(TAG, "adbCmdExec CAN'T FIND ANY DEVICE");
				return;
			}
		}
		
		try {			
			Process process = Runtime.getRuntime().exec(AndroidDeviceManager.getAdbPath() + cmdStr);  
			
			final BufferedReader batchInputStream = new BufferedReader(new InputStreamReader(process.getInputStream()));
			final  BufferedReader  batchErrorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
			
			new Thread(new Runnable() {
				String line;
				public void run() {
					try {
						while((line=batchErrorReader.readLine()) != null) {
							Log.d("Err", line);	       		        
						}
					} catch (IOException e) {
						e.printStackTrace();  
					}
				}
			}).start();
			
			new Thread(new Runnable() {
				String line;
				public void run() {
					try {
						while((line=batchInputStream.readLine()) != null) {
							Log.d("", line);	       	
						}
					} catch (IOException e) {
						e.printStackTrace();  
					}
				}
			}).start();
			
			int i = process.waitFor();
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}	
}
